package com.hn658.groupshare.account.dao.redis.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import com.hn658.framework.dataaccess.redis.AbstractRedisDAO;
import com.hn658.groupshare.account.dao.redis.IUserAccountRedisDao;
import com.hn658.groupshare.account.entity.UserAccountEO;
import com.hn658.groupshare.common.utils.RedisKeyHelper;

@Repository
public class UserAccountRedisDaoImpl extends AbstractRedisDAO<UserAccountEO>
		implements IUserAccountRedisDao {

	@Autowired
	@Qualifier("redisTemplate")
	@Override
	public void setTemplate(StringRedisTemplate template) {
		super.template = template;
	};

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void saveUserInfo(UserAccountEO userInfo,List<String> fields) {
		// 将userInfo保存至Redis
		BoundHashOperations<String, String, String> userOps = template
				.boundHashOps(RedisKeyHelper.frontendAccountUserInfo(userInfo.getId()));
		// 将user类转换为map,用于存储user属性
		Map userMap = new BeanMap(userInfo);
		// 保存user的所有属性
		for (Map.Entry entry : (Set<Map.Entry>) userMap.entrySet()) {
			// 排出情况
			if (entry.getValue() == null || "class".equals(entry.getKey())) {
				continue;
			}
			//
			if (fields!=null && fields.contains(entry.getKey())) {
				continue;
			}
			// 按类型存储
			if (entry.getValue() instanceof Date) {
				Date date = (Date) entry.getValue();
				userOps.put(entry.getKey().toString(), date.getTime() + "");
			} else {
				userOps.put(entry.getKey().toString(), entry.getValue()
						.toString());
			}
		}
	}

	@Override
	public UserAccountEO getUserById(Long id) {
		if (null == id) {
			return null;
		}
		// 从redis中获取对应uid的UserInfo
		BoundHashOperations<String, String, String> userOps = template
				.boundHashOps(RedisKeyHelper.frontendAccountUserInfo(id));
		return userOps.entries().isEmpty() != true ? mapper.fromHash(userOps
				.entries()) : null;
	}

	@Override
	public UserAccountEO getUserByMobilePhone(String mobilePhone) {
		// 先获取loginAccount对应的Uid
		String uid = valueOps.get(RedisKeyHelper.frontendMobilePhoneUid(StringUtils.lowerCase(mobilePhone)));
		if (StringUtils.isEmpty(uid)) {
			return null;
		}
		// 从Redis中获取对应uid的UserInfo
		return this.getUserById(Long.parseLong(uid));
	}

	@Override
	public UserAccountEO getUserByAuth(String authkey) {
		String uid = valueOps.get(RedisKeyHelper.frontendAuthkeyUid(authkey));
		if (StringUtils.isEmpty(uid)) {
			return null;
		}
		return this.getUserById(Long.parseLong(uid));
	}

	public void delete(Long uid) {
		String authkey = valueOps.get(RedisKeyHelper.frontendUidAuthkey(uid));
		
		BoundHashOperations<String, String, String> userOps = template
				.boundHashOps(RedisKeyHelper.frontendAccountUserInfo(uid));
		
		String mobilePhone = userOps.get("mobilePhone");
		if (StringUtils.isNotEmpty(authkey)) {
			template.delete(RedisKeyHelper.frontendAuthkeyUid(authkey));
		}
		if (StringUtils.isNotEmpty(mobilePhone)) {
			template.delete(RedisKeyHelper.frontendMobilePhoneUid(mobilePhone));
		}
		
		template.delete(RedisKeyHelper.frontendAccountUserInfo(uid));
		
	}

	@Override
	public void setUserAuthkey(Long uid, String authkey) {
		// 删除原先的authkey
		String origAuthkey = valueOps.get(RedisKeyHelper.backendUserAuth(uid.toString()));
		if(StringUtils.isNotEmpty(origAuthkey)){
			template.delete(RedisKeyHelper.frontendAuthkeyUid(origAuthkey));
		}
		
		// 创建authkey
		valueOps.set(RedisKeyHelper.frontendAuthkeyUid(authkey), Long.toString(uid));
		valueOps.set(RedisKeyHelper.frontendUidAuthkey(uid), authkey);
	}

	@Override
	public void removeUserAuthkey(Long uid) {
		String authkey = valueOps.get(RedisKeyHelper.frontendUidAuthkey(uid));
		
		template.delete(RedisKeyHelper.frontendUidAuthkey(uid));
		template.delete(RedisKeyHelper.frontendAuthkeyUid(authkey));
	}

	@Override
	public Long getUidByAuthkey(String authkey) {
		String uid = valueOps.get(RedisKeyHelper.frontendAuthkeyUid(authkey));
		if(StringUtils.isNotBlank(uid)){
			return new Long(uid);
		}else{
			return null;
		}
	}

}
